package com.gzc.just.play.last.war.allinone.controller;

import com.gzc.just.play.last.war.common.share.service.DataAccessManager;
import com.gzc.just.play.last.war.common.share.util.EnvironmentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * All-in-One模式控制器
 * 提供环境信息和健康检查的REST接口
 */
@RestController
@RequestMapping("/api/all-in-one")
public class AllInOneController {
    
    @Autowired
    private DataAccessManager dataAccessManager;
    
    @Autowired
    private Environment environment;
    
    /**
     * 获取环境信息
     */
    @GetMapping("/environment")
    public Map<String, Object> getEnvironmentInfo() {
        Map<String, Object> info = new HashMap<>();
        
        info.put("activeProfile", EnvironmentUtils.getActiveProfile(environment));
        info.put("isDevelopment", EnvironmentUtils.isDevelopment(environment));
        info.put("isProduction", EnvironmentUtils.isProduction(environment));
        info.put("databaseType", EnvironmentUtils.getDatabaseType(environment));
        info.put("cacheType", EnvironmentUtils.getCacheType(environment));
        info.put("supportsAllInOne", EnvironmentUtils.supportsAllInOneMode(environment));
        info.put("environmentSummary", EnvironmentUtils.getEnvironmentSummary(environment));
        info.put("isValid", EnvironmentUtils.validateEnvironment(environment));
        
        return info;
    }
    
    /**
     * 健康检查
     */
    @GetMapping("/health")
    public DataAccessManager.HealthCheckResult healthCheck() {
        return dataAccessManager.healthCheck();
    }
    
    /**
     * 获取数据访问管理器信息
     */
    @GetMapping("/manager-info")
    public Map<String, Object> getManagerInfo() {
        Map<String, Object> info = new HashMap<>();
        
        info.put("environmentInfo", dataAccessManager.getEnvironmentInfo());
        info.put("isDevelopment", dataAccessManager.isDevelopment());
        info.put("isProduction", dataAccessManager.isProduction());
        info.put("dataAccessType", dataAccessManager.getDataAccessService().getDatabaseType());
        info.put("cacheType", dataAccessManager.getCacheService().getCacheType());
        
        return info;
    }
}
